package com.example.qx.wda;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.qx.R;
import com.example.qx.database.data;
import com.example.qx.lei.cos;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.zhangmi;
import com.example.qx.main;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

public class wd extends Fragment {
    private Button grcj,tcdl;
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
    private ImageView imageView;
    private TextView textView;
    private data data=new data();
    private Uri uri= null;
    private cos cos=new cos();
    public wd() throws SQLException, ClassNotFoundException, IllegalAccessException, java.lang.InstantiationException {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup,@Nullable Bundle savedInstanceState) {
       View view=inflater.inflate(R.layout.wd,viewGroup,false);
       imageView=view.findViewById(R.id.touxiangaaa);
           textView=view.findViewById(R.id.wangming);
          tcdl=view.findViewById(R.id.tuichudenglu);
        final Connection[] connection = new Connection[1];
        final leilianxr[] leilianxr = new leilianxr[1];
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                catch (IllegalAccessException | ClassNotFoundException |
                       java.lang.InstantiationException e) {
                    throw new RuntimeException(e);}
                try {
                   connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");
                  leilianxr[0] =data.getgrxx(zhangmi.getZhang(),connection[0]);

                } catch (SQLException e) {throw new RuntimeException(e);}
                latch.countDown();
            }
        }).start();
        try{
            latch.await();
            connection[0].close();
        }
        catch (InterruptedException | SQLException e)
        {e.printStackTrace();}
        textView.setText(leilianxr[0].getMingzi());
        tcdl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), main.class);
                File file=getActivity().getDir("youqishuju",getActivity().MODE_PRIVATE);
                if(file.exists())
                    file.delete();
                startActivity(intent);
        }});

        ContentResolver contentResolver= Objects.requireNonNull(getContext()).getContentResolver();

            if(!Objects.equals(leilianxr[0].getTouxiang(), "s")&& !Objects.equals(leilianxr[0].getTouxiang(), ""))
            {
                uri = Uri.parse(leilianxr[0].getTouxiang());
                String a = contentResolver.getType(uri);
                if (a.startsWith("image/")) {

                    String b=String.valueOf(zhangmi.getZhang());
                    b=b+"tx";
                    File filee=getContext().getDir("youqishuju",getContext().MODE_PRIVATE);
                    String abc= filee.getAbsolutePath();
                    File filel=new File(abc+"/"+b);
                    if(!filel.exists()) {
                        cos.get(b, getActivity());
                    }
                        Bitmap bitmap = BitmapFactory.decodeFile(abc+"/"+b);
                        imageView.setImageBitmap(bitmap);
                }
            }

        grcj=view.findViewById(R.id.grcj);
        grcj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), wd_grcj.class);
                startActivity(intent);
            }
        });

        return view;

    }


    }

